﻿using System;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.UserModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        FrmUserManage ob_FrmUserManage;
        FrmPswProInfo ob_FrmPswProInfo;
        FrmGoodsPurchase ob_FrmPurchaseGoods;
        FrmGoodsQuery ob_FrmGoodsQuery;
        FrmGoodsSale ob_FrmGoodsSale;
        FrmSaleDataQuery ob_FrmSaleDataQuery;
        FrmSaleDataCount ob_FrmSaleDataCount;
        FrmInventoryAlarm ob_FrmInventoryAlarm;
        FrmInventoryQuery ob_FrmInventoryQuery;
        FrmBackupData ob_FrmBackupData;
        FrmRestoreData ob_FrmRestoreData;
        FrmHelp ob_FrmHelp;

        private void FrmMain_Load(object sender, EventArgs e)
        {
            string userName = UserManager.userInfo[0];
            UserPurviewManager userPurviewManager = new UserPurviewManager();
            UserPurview userPurview = new UserPurview();
            userPurview = userPurviewManager.GetUserPurview(userName);
            tsmiUserManage.Enabled = userPurview.UserManage == "是" ? true : false;
            tsbUserManage.Enabled = tsmiUserManage.Enabled;
            tsmiPurchaseGoods.Enabled = userPurview.PurchaseGoods == "是" ? true : false;
            tsbPurchaseGoods.Enabled = tsmiPurchaseGoods.Enabled;
            tsmiGoodsQuery.Enabled = userPurview.GoodsQuery == "是" ? true : false;
            tsbGoodsQuery.Enabled = tsmiGoodsQuery.Enabled;
            tsmiGoodsSale.Enabled = userPurview.GoodsSale == "是" ? true : false;
            tsbGoodsSale.Enabled = tsmiGoodsSale.Enabled;
            tsmiSaleDataQuery.Enabled = userPurview.SaleDataQuery == "是" ? true : false;
            tsbSaleDataQuery.Enabled = tsmiSaleDataQuery.Enabled;
            tsmiSaleDataCount.Enabled = userPurview.SaleDataCount == "是" ? true : false;
            tsbSaleDataCount.Enabled = tsmiSaleDataCount.Enabled;
            tsmiInventoryAlarm.Enabled = userPurview.InventoryAlarm == "是" ? true : false;
            tsbInventoryAlarm.Enabled = tsmiInventoryAlarm.Enabled;
            tsmiInventoryQuery.Enabled = userPurview.InventoryQuery == "是" ? true : false;
            tsbInventoryQuery.Enabled = tsmiInventoryQuery.Enabled;
            tsmiBackupData.Enabled = userPurview.BackupData == "是" ? true : false;
            tsbBackupData.Enabled = tsmiBackupData.Enabled;
            tsmiRestoreData.Enabled = userPurview.RestoreData == "是" ? true : false;
            tsbRestoreData.Enabled = tsmiRestoreData.Enabled;
            tsslInfo.Text = "当前用户： " + UserManager.userInfo[0] + " （" + UserManager.userInfo[2] + "）";
            tsslInfo.Width = (this.Width - 30) / 2;
            tsslDate.Text = DateTime.Now.ToLongDateString();
            tsslDate.Width = (this.Width - 30) / 2;
            this.WindowState = FormWindowState.Maximized;
        }

        private void FrmMain_SizeChanged(object sender, EventArgs e)
        {
            tsslInfo.Width = (this.Width - 30) / 2;
            tsslDate.Width = (this.Width - 30) / 2;
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ob_FrmUserManage != null && !ob_FrmUserManage.IsDisposed)
            {
                MessageBox.Show("请先关闭用户管理子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmUserManage.Activate();
            }
            else if (ob_FrmPswProInfo != null && !ob_FrmPswProInfo.IsDisposed)
            {
                MessageBox.Show("请先关闭密码保护子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmPswProInfo.Activate();
            }
            else if (ob_FrmPurchaseGoods != null && !ob_FrmPurchaseGoods.IsDisposed)
            {
                MessageBox.Show("请先关闭进货管理管理子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmPurchaseGoods.Activate();
            }
            else if (ob_FrmGoodsQuery != null && !ob_FrmGoodsQuery.IsDisposed)
            {
                MessageBox.Show("请先关闭商品查询子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmGoodsQuery.Activate();
            }
            else if (ob_FrmGoodsSale != null && !ob_FrmGoodsSale.IsDisposed)
            {
                MessageBox.Show("请先关闭商品销售子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmGoodsSale.Activate();
            }
            else if (ob_FrmSaleDataQuery != null && !ob_FrmSaleDataQuery.IsDisposed)
            {
                MessageBox.Show("请先关闭销售数据查询子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmSaleDataQuery.Activate();
            }
            else if (ob_FrmSaleDataCount != null && !ob_FrmSaleDataCount.IsDisposed)
            {
                MessageBox.Show("请先关闭销售数据统计子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmSaleDataCount.Activate();
            }
            else if (ob_FrmInventoryAlarm != null && !ob_FrmInventoryAlarm.IsDisposed)
            {
                MessageBox.Show("请先关闭库存报警子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmInventoryAlarm.Activate();
            }
            else if (ob_FrmInventoryQuery != null && !ob_FrmInventoryQuery.IsDisposed)
            {
                MessageBox.Show("请先关闭库存查询子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmInventoryQuery.Activate();
            }
            else if (ob_FrmBackupData != null && !ob_FrmBackupData.IsDisposed)
            {
                MessageBox.Show("请先关闭数据库备份子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmBackupData.Activate();
            }
            else if (ob_FrmRestoreData != null && !ob_FrmRestoreData.IsDisposed)
            {
                MessageBox.Show("请先关闭数据库恢复子系统再试!", "重试");
                e.Cancel = true;
                ob_FrmRestoreData.Activate();
            }
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void tsmiUserManage_Click(object sender, EventArgs e)
        {
            if (ob_FrmUserManage == null || ob_FrmUserManage.IsDisposed)
            {
                ob_FrmUserManage = new FrmUserManage();
                ob_FrmUserManage.MdiParent = this;
                ob_FrmUserManage.Show();
            }
            else
            {
                ob_FrmUserManage.Activate();
            }
        }

        private void tsmiPswProInfo_Click(object sender, EventArgs e)
        {
            if (ob_FrmPswProInfo == null || ob_FrmPswProInfo.IsDisposed)
            {
                ob_FrmPswProInfo = new FrmPswProInfo();
                ob_FrmPswProInfo.MdiParent = this;
                ob_FrmPswProInfo.Show();
            }
            else
            {
                ob_FrmPswProInfo.Activate();
            }
        }

        private void tsmiChangeUser_Click(object sender, EventArgs e)
        {
            if (ob_FrmUserManage != null && !ob_FrmUserManage.IsDisposed)
            {
                MessageBox.Show("请先关闭用户管理子系统再试!", "重试");
                ob_FrmUserManage.Activate();
            }
            else if (ob_FrmPswProInfo != null && !ob_FrmPswProInfo.IsDisposed)
            {
                MessageBox.Show("请先关闭密码保护子系统再试!", "重试");
                ob_FrmPswProInfo.Activate();
            }
            else if (ob_FrmPurchaseGoods != null && !ob_FrmPurchaseGoods.IsDisposed)
            {
                MessageBox.Show("请先关闭进货管理管理子系统再试!", "重试");
                ob_FrmPurchaseGoods.Activate();
            }
            else if (ob_FrmGoodsQuery != null && !ob_FrmGoodsQuery.IsDisposed)
            {
                MessageBox.Show("请先关闭商品查询子系统再试!", "重试");
                ob_FrmGoodsQuery.Activate();
            }
            else if (ob_FrmGoodsSale != null && !ob_FrmGoodsSale.IsDisposed)
            {
                MessageBox.Show("请先关闭商品销售子系统再试!", "重试");
                ob_FrmGoodsSale.Activate();
            }
            else if (ob_FrmSaleDataQuery != null && !ob_FrmSaleDataQuery.IsDisposed)
            {
                MessageBox.Show("请先关闭销售数据查询子系统再试!", "重试");
                ob_FrmSaleDataQuery.Activate();
            }
            else if (ob_FrmSaleDataCount != null && !ob_FrmSaleDataCount.IsDisposed)
            {
                MessageBox.Show("请先关闭销售数据统计子系统再试!", "重试");
                ob_FrmSaleDataCount.Activate();
            }
            else if (ob_FrmInventoryAlarm != null && !ob_FrmInventoryAlarm.IsDisposed)
            {
                MessageBox.Show("请先关闭库存报警子系统再试!", "重试");
                ob_FrmInventoryAlarm.Activate();
            }
            else if (ob_FrmInventoryQuery != null && !ob_FrmInventoryQuery.IsDisposed)
            {
                MessageBox.Show("请先关闭库存查询子系统再试!", "重试");
                ob_FrmInventoryQuery.Activate();
            }
            else if (ob_FrmBackupData != null && !ob_FrmBackupData.IsDisposed)
            {
                MessageBox.Show("请先关闭数据库备份子系统再试!", "重试");
                ob_FrmBackupData.Activate();
            }
            else if (ob_FrmRestoreData != null && !ob_FrmRestoreData.IsDisposed)
            {
                MessageBox.Show("请先关闭数据库恢复子系统再试!", "重试");
                ob_FrmRestoreData.Activate();
            }
            else
            {
                FrmLogin ob_FrmLogin = new FrmLogin();
                ob_FrmLogin.Show();
                this.Hide();
            }
        }

        private void tsmiEixt_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tsmiPurchaseGoods_Click(object sender, EventArgs e)
        {
            if (ob_FrmPurchaseGoods == null || ob_FrmPurchaseGoods.IsDisposed)
            {
                ob_FrmPurchaseGoods = new FrmGoodsPurchase();
                ob_FrmPurchaseGoods.MdiParent = this;
                ob_FrmPurchaseGoods.Show();
            }
            else
            {
                ob_FrmPurchaseGoods.Activate();
            }
        }

        private void tsmiGoodsQuery_Click(object sender, EventArgs e)
        {
            if (ob_FrmGoodsQuery == null || ob_FrmGoodsQuery.IsDisposed)
            {
                ob_FrmGoodsQuery = new FrmGoodsQuery();
                ob_FrmGoodsQuery.MdiParent = this;
                ob_FrmGoodsQuery.Show();
            }
            else
            {
                ob_FrmGoodsQuery.Activate();
            }
        }

        private void tsmiGoodsSale_Click(object sender, EventArgs e)
        {
            if (ob_FrmGoodsSale == null || ob_FrmGoodsSale.IsDisposed)
            {
                ob_FrmGoodsSale = new FrmGoodsSale();
                ob_FrmGoodsSale.MdiParent = this;
                ob_FrmGoodsSale.Show();
            }
            else
            {
                ob_FrmGoodsSale.Activate();
            }
        }

        private void tsmiSaleDataQuery_Click(object sender, EventArgs e)
        {
            if (ob_FrmSaleDataQuery == null || ob_FrmSaleDataQuery.IsDisposed)
            {
                ob_FrmSaleDataQuery = new FrmSaleDataQuery();
                ob_FrmSaleDataQuery.MdiParent = this;
                ob_FrmSaleDataQuery.Show();
            }
            else
            {
                ob_FrmSaleDataQuery.Activate();
            }
        }

        private void tsmiSaleDataCount_Click(object sender, EventArgs e)
        {
            if (ob_FrmSaleDataCount == null || ob_FrmSaleDataCount.IsDisposed)
            {
                ob_FrmSaleDataCount = new FrmSaleDataCount();
                ob_FrmSaleDataCount.MdiParent = this;
                ob_FrmSaleDataCount.Show();
            }
            else
            {
                ob_FrmSaleDataCount.Activate();
            }
        }

        private void tsmiInventoryAlarm_Click(object sender, EventArgs e)
        {
            if (ob_FrmInventoryAlarm == null || ob_FrmInventoryAlarm.IsDisposed)
            {
                ob_FrmInventoryAlarm = new FrmInventoryAlarm();
                ob_FrmInventoryAlarm.MdiParent = this;
                ob_FrmInventoryAlarm.Show();
            }
            else
            {
                ob_FrmInventoryAlarm.Activate();
            }
        }

        private void tsmiInventoryQuery_Click(object sender, EventArgs e)
        {
            if (ob_FrmInventoryQuery == null || ob_FrmInventoryQuery.IsDisposed)
            {
                ob_FrmInventoryQuery = new FrmInventoryQuery();
                ob_FrmInventoryQuery.MdiParent = this;
                ob_FrmInventoryQuery.Show();
            }
            else
            {
                ob_FrmInventoryQuery.Activate();
            }
        }

        private void tsmiBackupData_Click(object sender, EventArgs e)
        {
            if (ob_FrmBackupData == null || ob_FrmBackupData.IsDisposed)
            {
                ob_FrmBackupData = new FrmBackupData();
                ob_FrmBackupData.MdiParent = this;
                ob_FrmBackupData.Show();
            }
            else
            {
                ob_FrmBackupData.Activate();
            }
        }

        private void tsmiRestoreData_Click(object sender, EventArgs e)
        {
            if (ob_FrmRestoreData == null || ob_FrmRestoreData.IsDisposed)
            {
                ob_FrmRestoreData = new FrmRestoreData();
                ob_FrmRestoreData.MdiParent = this;
                ob_FrmRestoreData.Show();
            }
            else
            {
                ob_FrmRestoreData.Activate();
            }
        }

        private void tsmiHelp_Click(object sender, EventArgs e)
        {
            if (ob_FrmHelp == null || ob_FrmHelp.IsDisposed)
            {
                ob_FrmHelp = new FrmHelp();
                ob_FrmHelp.MdiParent = this;
                ob_FrmHelp.Show();
            }
            else
            {
                ob_FrmHelp.Activate();
            }
        }

        private void tsmiAbout_Click(object sender, EventArgs e)
        {
            AppCore.FrmAbout.MdiParent = this;
            AppCore.FrmAbout.Show();
            AppCore.FrmAbout.Activate();
        }

        private void tsMain_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int n = tsMain.Items.IndexOf(e.ClickedItem);
            switch (n)
            {
                case 0:
                    tsmiUserManage_Click(sender, e);
                    break;
                case 1:
                    tsmiPswProInfo_Click(sender, e);
                    break;
                case 3:
                    tsmiPurchaseGoods_Click(sender, e);
                    break;
                case 4:
                    tsmiGoodsQuery_Click(sender, e);
                    break;
                case 6:
                    tsmiGoodsSale_Click(sender, e);
                    break;
                case 7:
                    tsmiSaleDataQuery_Click(sender, e);
                    break;
                case 8:
                    tsmiSaleDataCount_Click(sender, e);
                    break;
                case 10:
                    tsmiInventoryAlarm_Click(sender, e);
                    break;
                case 11:
                    tsmiInventoryQuery_Click(sender, e);
                    break;
                case 13:
                    tsmiBackupData_Click(sender, e);
                    break;
                case 14:
                    tsmiRestoreData_Click(sender, e);
                    break;
                case 16:
                    System.Diagnostics.Process.Start("calc.exe");
                    break;
                case 18:
                    tsmiChangeUser_Click(sender, e);
                    break;
                case 20:
                    tsmiEixt_Click(sender, e);
                    break;
            }
        }
    }
}
